﻿var game = null;

$(window).resize(function () {
    if (game != null && game.getView().isActive) {
        game.resize();
    }
});

$(document).bind("pageinit", function () {
    $(".ui-loader").hide();
    game = Game.newInstance();
    game.getView().setup();
    game.getView().paint();
    game.getView().pause();
    game.currentView = "login";
    game.getView().setup();
    //game.getView().resume();
    //setTimeout("game.getView().teardown()", 10000);
    //setTimeout("game.getView().setup()", 15000);
});

/*<div class="ui-loader ui-corner-all ui-body-a ui-loader-default"><span class="ui-icon ui-icon-loading"></span><h1>loading</h1></div>*/

var Player = Base.extend({
    initialize: function initialize() {
        this.name = "Anonymous",
        this.oid = 0
    }
});

var Game = Base.extend({
    initialize: function initialize() {
        this.views = { "fourkill": FourKillGameView.newInstance(this), "login": LoginView.newInstance(this) };
        this.svc = FourKillDataService.newInstance();
        this.player = Player.newInstance();
        this.currentView = "fourkill";
    },
    getView: function getView(viewName) {
        viewName = viewName == null ? this.currentView : viewName;
        return this.views[viewName];
    },
    changeView: function changeView(viewName) {
        this.getView().teardown();
        this.currentView = viewName;
        if (this.getView().isActive) this.getView().resume();
        else this.getView().setup();
    },
    setup: function setup() {
        this.views[this.currentView].setup();
    },
    teardown: function teardown() {
        this.views[this.currentView].teardown();
    },
    resize: function resize() {
        for (viewId in this.views) this.views[viewId].resize();
    },
    notify: function notify(msgType, msgContent) {
        if (msgType == "login") {
            alert(msgContent.email + " ***" + msgContent.password.length + "***");
            if (msgContent.email != "")
                this.changeView(msgContent.targetView);
        }
        if (msgType == "change-view") {
            this.changeView(msgContent);
        }
    }
});

var FourKillDataService = Base.extend({
    initialize: function initialize() {
    },
    login: function login(name, password) {
        return {
            id: 123,
            name: name,
            isAuthenticated: true,
            latitude: 39.955959,
            longitude: -86.013181,
            bearing: util._90
        };
    },
    update: function update(playerId, latitude, longitude) {
        // update the server with players current location
    },
    getStaticObjectsInLocation: function update(playerId, latitude, longitude) {
        // update the server with players current location
        return [{
            id: 'obj001',
            name: "Justin's House",
            latitude: 39.960753,
            longitude: -86.028517,
            bearing: util._90,
            sprite: "landmark"
        },
        {
            id: 'obj002',
            name: 'Georges House',
            latitude: 40.026871,
            longitude: -86.394355,
            bearing: util._90,
            sprite: "landmark"
        },
        {
            id: 'obj003',
            name: "Fishers Target",
            latitude: 39.959693,
            longitude: -86.009892,
            bearing: util._90,
            sprite: "landmark"
        },
        {
            id: 'obj004',
            name: "Don Greenwood & Associates",
            latitude: 39.955943,
            longitude: -86.013181,
            bearing: util._90,
            sprite: "landmark"
        },
        ];
    }
});